$(document).ready(function(){
	$("#bthAddPhase").click(function(){
		popupAddPhaseDialog();
	});
	
	initProjectPhaseDialog();
});

function initProjectPhaseDialog() {
	var projectId = JSON.parse($("#tableDetail").attr("dataStr")).projectId;
	$("#projectPhaseDialog #projectId").val(projectId);
	$("#projectPhaseDialog #projectName").val($("#tableDetail #name").text());
}

var projectPhaseDialog = $("#projectPhaseDialog").dialog({
    autoOpen: false,
    height:200,
    width: 300,
    modal: true,
	close: function() {}
});


function popupAddPhaseDialog() {
    $("#projectPhaseDialog #id").val("");
    initSemeterSelection();
    
    projectPhaseDialog = projectPhaseDialog.dialog({
		label: "开启新学期",
	    buttons: {
	    	"开启新学期": addProjectPhase,
	    	Cancel: function() {
	    		projectPhaseDialog.dialog("close");
	    	}
	    }
	});
    projectPhaseDialog.dialog("open");
}

function initSemeterSelection() {
	var projectId = $("#projectPhaseDialog #projectId").val();
    var url = getAncestorPath(location.pathname, 1) + projectId + "/phases?filter=uncreated";
	$.ajax({
	    type: "GET",
	    url: url,
	    dataType: "json",
	    success: function (resp) {
	        if (resp.code == "0") {
	            var semesterSelectObj = $("#projectPhaseDialog #semester");
	            semesterSelectObj.empty();
	            $.each(resp.data,function(index, item) {
	            	var optionHtml = "<option value='" + item.id + "'>" + item.name + "</option>";
	            	semesterSelectObj.append(optionHtml);
	            });
	        } else {
	            alert("服务器返回出错信息：\n\n" + resp.message);
	        }
	    },
	    error: function () { alert("Sorry，服务器处理请求出错，请联系IT运维人员。"); }
	});
}

function addProjectPhase() {
	var projectId = $("#projectPhaseDialog #projectId").val();
	var semesterId =  $("#projectPhaseDialog #semester").val();
	var postData = {projectId:projectId, semesterId:semesterId};
	var url = location.pathname + "/../../project-phases";
    $.ajax({
        type: "POST",
        url: url,
        data: JSON.stringify(postData),
        dataType: "json",
        contentType:"application/json",
        success: function (resp) {
            if (resp.code == "0") {
            	projectPhaseDialog.dialog("close");
            	reloadProjectPhases();
            } else {
            	alert("服务器返回出错信息：\n\n" + resp.message);
            }
        },
        error: function() { alert("Sorry，服务器处理请求出错，请联系IT运维人员。"); }
    });    
}

function reloadProjectPhases() {
	var projectId = $("#projectPhaseDialog #projectId").val();
	var url = location.pathname + "/../../project-phases?projectId=" + projectId;
    $.ajax({
        type: "GET",
        url: url,
        contentType:"application/json",
        success: function (resp) {
            if (resp.code == "0") {
            	var tableBodyObj = $("#tableProjectPhases tbody");
            	tableBodyObj.html("");
                $.each(resp.data,function(index, item) {
                	var rowHtml = "<tr dataStr=" + JSON.stringify(item) + ">";
                	rowHtml += "<td>" + item.semesterName + "</td>";
                	rowHtml += "<td>" + item.status + "</td>";
                	//rowHtml += "<td><span class='clickable' onclick='popupEditDialog(event)'>edit</span></td>";
                	rowHtml += "</tr>";
                	tableBodyObj.append(rowHtml);
                });
            } else {
            	alert("服务器返回出错信息：\n\n" + resp.message);
            }
        },
        error: function() { alert("Sorry，服务器处理请求出错，请联系IT运维人员。"); }
    });    
}

